import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import html from 'vite-plugin-html' // 为index.html提供minify和基于EJS模板功能

import { resolve } from "path";
//import WindiCSS from 'vite-plugin-windicss'
import Components from 'unplugin-vue-components/vite'
import {ElementPlusResolver} from 'unplugin-vue-components/resolvers'
import { fileURLToPath } from 'url';

  const htmlPlugin  = createHtmlPlugin({
    minify: isBuild,
    inject: {
      // Inject data into ejs template
      data: {
        title: VITE_GLOB_APP_TITLE,
        injectScript: `<script type="text/javascript" src="https://agent-gate-${VITE_GLOB_TR_REGION == '2'?'test-':''}${VITE_GLOB_TR_REGION}.cloud/js/ctilinks.js"></script>`
      },
      }
    }); 
  

// https://vitejs.dev/config/
export default defineConfig(({command,mode })=>{
 const env=loadEnv(mode ,process.cwd()) 
//  console.log(env.VITE_APP_TITLE)//获取环境变量 
//  console.log(env.VITE_APP_API_BASEURL)//获取环境变量 
 return{  
   //envPrefix:  ['VUE','VITE'],   // 环境变量前缀,默认只会暴露VITE开头变量，定义后可暴露VUE开头变量
   define: {
    'process.env': {
      'VUE_APP_TITLE' : env.VITE_APP_TITLE,
      'VUE_APP_API_BASEURL' : env.VITE_APP_API_BASEURL,
      'VUE_APP_PORT' : env.VITE_APP_PORT,
      'VUE_APP_PROXY' :env.VITE_APP_PROXY,
    }  
  }, 
  plugins: [
    vue(),
    //WindiCSS(),
    Components({
      resolvers:[ElementPlusResolver()]
    }),   
  ],  
  resolve: {
    // 配置路径别名
    alias: {
        '@': resolve(__dirname, 'src'), // 设置 `@` 指向 `src` 目录     
        'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js'
      }, 
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
    },
    base: './',// 设置打包路径
    server:
    { 
      //allowedHosts: 'all',		
      open:true,//自动打开浏览器
      port:env.VITE_APP_PORT, //挂载端口
      //设置代理，根据我们项目实际情况配置
      proxy: {
        '/api': {
          target: env.VITE_APP_API_BASEURL,
          ws: true,
          changeOrigin: true,
          secure: false,    
          rewrite: (path) => path.replace(/^\/api/, '/')
        }
      }
    },
    build: {

    },
  }
})